import axios from "axios";

function getOS() {
  var sUserAgent = navigator.userAgent;
  var isWin = navigator.platform == "Win32" || navigator.platform == "Windows";
  var isMac =
    navigator.platform == "Mac68K" ||
    navigator.platform == "MacPPC" ||
    navigator.platform == "Macintosh" ||
    navigator.platform == "MacIntel";
  if (isMac) return "Mac";
  var isUnix = navigator.platform == "X11" && !isWin && !isMac;
  if (isUnix) return "Unix";
  var isLinux = String(navigator.platform).indexOf("Linux") > -1;
  if (isLinux) return "Linux";
  if (isWin) {
    var isWin2K =
      sUserAgent.indexOf("Windows NT 5.0") > -1 ||
      sUserAgent.indexOf("Windows 2000") > -1;
    if (isWin2K) return "Win2000";
    var isWinXP =
      sUserAgent.indexOf("Windows NT 5.1") > -1 ||
      sUserAgent.indexOf("Windows XP") > -1;
    if (isWinXP) return "WinXP";
    var isWin2003 =
      sUserAgent.indexOf("Windows NT 5.2") > -1 ||
      sUserAgent.indexOf("Windows 2003") > -1;
    if (isWin2003) return "Win2003";
    var isWinVista =
      sUserAgent.indexOf("Windows NT 6.0") > -1 ||
      sUserAgent.indexOf("Windows Vista") > -1;
    if (isWinVista) return "WinVista";
    var isWin7 =
      sUserAgent.indexOf("Windows NT 6.1") > -1 ||
      sUserAgent.indexOf("Windows 7") > -1;
    if (isWin7) return "Win7";
    var isWin10 =
      sUserAgent.indexOf("Windows NT 10") > -1 ||
      sUserAgent.indexOf("Windows 10") > -1;
    if (isWin10) return "Win10";
  }
  return "other";
}
function browse() {
  var browser = {};
  var userAgent = navigator.userAgent.toLowerCase();
  var s;
  (s = userAgent.match(/msie ([\d.]+)/))
    ? (browser.ie = s[1])
    : (s = userAgent.match(/firefox\/([\d.]+)/))
    ? (browser.firefox = s[1])
    : (s = userAgent.match(/chrome\/([\d.]+)/))
    ? (browser.chrome = s[1])
    : (s = userAgent.match(/opera.([\d.]+)/))
    ? (browser.opera = s[1])
    : (s = userAgent.match(/version\/([\d.]+).*safari/))
    ? (browser.safari = s[1])
    : 0;
  var version = "";
  if (browser.ie) {
    version = "IE " + browser.ie;
  } else {
    if (browser.firefox) {
      version = "Firefox " + browser.firefox;
    } else {
      if (browser.chrome) {
        version = "Chrome " + browser.chrome;
      } else {
        if (browser.opera) {
          version = "Opera " + browser.opera;
        } else {
          if (browser.safari) {
            version = "Safari " + browser.safari;
          } else {
            version = "未知浏览器";
          }
        }
      }
    }
  }
  return version;
}

const http = axios.create({
  // baseURL: '/mainIp',
  baseURL: "/",
  timeout: 30000,
});
const RequestMode = ["delete", "get", "post", "put"];
http.interceptors.request.use((config) => {
  if (sessionStorage.getItem("ClientName") == null) {
    sessionStorage.setItem(
      "ClientName",
      getOS().toString() + "-" + browse().toString()
    );
  }
  console.log("config", config);
  if (config.url.indexOf("gui") < 0) {
    if (config.method.toLowerCase() === "get" && config.params !== undefined) {
      config.params.ClientName = sessionStorage.getItem("ClientName");
      config.params.ClientIP = sessionStorage.getItem("ClientIP");
    } else if (
      config.data !== undefined &&
      RequestMode.indexOf(config.method.toLowerCase()) !== -1
    ) {
      config.data.ClientName = getOS().toString() + "-" + browse().toString();
    }
  }
  return config;
});
http.interceptors.response.use((config) => {
  if (
    config.data.ClientIP !== undefined &&
    sessionStorage.getItem("ClientIP") !== config.data.ClientIP &&
    config.data.ClientIP !== ""
  ) {
    sessionStorage.removeItem("ClientIP");
    sessionStorage.setItem("ClientIP", config.data.ClientIP);
  }
  return config;
});

export default http;
